package com.theo.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.theo.entity.RefutationInfo;
import com.theo.entity.RumourInfo;
import com.theo.service.RefutationInfoService;
import com.theo.service.RumourInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 方涛
 * @since 2021-02-02
 */
@Controller
@RequestMapping("/refutation-info")
public class RefutationInfoController {

    @Autowired
    private RefutationInfoService refutationInfoService;
    @Autowired
    private RumourInfoService rumourInfoService;

    @RequestMapping("/show.do")
    public String show(Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 8);
        List<RefutationInfo> list = refutationInfoService.queryAllRefutationInfo();
        PageInfo pageInfo = new PageInfo(list, 2);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/refutation_list";
    }

    @RequestMapping("/show")
    public String showR(Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 8);
        List<RefutationInfo> list = refutationInfoService.queryAllRefutationInfo();
        PageInfo pageInfo = new PageInfo(list, 2);
        model.addAttribute("pageInfo", list);
        return "user/refutation_list";
    }

    @RequestMapping("/read")
    public String read(Model model,Integer id) {
        RefutationInfo refutationInfo = refutationInfoService.queryRefutationInfoById(id);
        System.out.println(refutationInfo);
        model.addAttribute("refutationInfo", refutationInfo);
        return "user/refutation_one";
    }
    @RequestMapping("/toInput.do")
    public String toInput(Model model, Integer id) {
        RumourInfo rumourInfo = rumourInfoService.queryRumourInfoById(id);
        model.addAttribute("rumourInfo", rumourInfo);
        return "admin/refutation_input";
    }

    @RequestMapping("/input.do")
    public String input(RefutationInfo refutationInfo) {
        RumourInfo rumourInfo = rumourInfoService.queryRumourInfoById(refutationInfo.getRumourId());
        refutationInfoService.saveRefutationInfo(refutationInfo);
        rumourInfo.setIsRefuted(1);
        rumourInfoService.updateRumourInfo(rumourInfo);
        return "redirect:/refutation-info/show.do";
    }

    @RequestMapping("/update.do")
    public String update(RefutationInfo refutationInfo) {
        refutationInfoService.updateRefutationInfo(refutationInfo);
        return "redirect:/refutation-info/show.do";
    }

    @RequestMapping("/delete.do")
    public String delete(Integer id,Integer rId) {
        RumourInfo rumourInfo = rumourInfoService.queryRumourInfoById(rId);
        refutationInfoService.deleteRefutationInfo(id);
        rumourInfo.setIsRefuted(0);
        rumourInfoService.updateRumourInfo(rumourInfo);
        return "redirect:/refutation-info/show.do";
    }
}

